package priv.pront.code.lanqiao.competition.course.date0402;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-04-02 14:58
 */
public class 离开中山路 {
    static int n, x1, x2, y1, y2;
    static char[][] map;
    static boolean[][] visited;
    static int[] dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
    static Queue<Info> queue = new LinkedList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        map = new char[n][n];
        visited = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            map[i] = scanner.next().toCharArray();
        }
        x1 = scanner.nextInt();
        y1 = scanner.nextInt();
        x2 = scanner.nextInt();
        y2 = scanner.nextInt();
        queue.offer(new Info(x1 - 1, y1 - 1, 0));
        visited[x1 - 1][y1 - 1] = true;
        while (!queue.isEmpty()) {
            Info cur = queue.poll();
            if (cur.x == x2 - 1 && cur.y == y2 - 1) {
                System.out.println(cur.steps);
                break;
            }
            for (int i = 0; i < 4; i++) {
                int nx = cur.x + dx[i];
                int ny = cur.y + dy[i];
                if (valid(nx, ny)) {
                    queue.offer(new Info(nx, ny, cur.steps + 1));
                    visited[nx][ny] = true;
                }
            }
        }
    }

    private static boolean valid(int x, int y) {
        return x >= 0 && y >= 0 && x < n && y < n && map[x][y] != '1' && !visited[x][y];
    }

    public static class Info {
        int x;
        int y;
        int steps;

        public Info(int x, int y, int steps) {
            this.x = x;
            this.y = y;
            this.steps = steps;
        }
    }
}
